package com.cskaoyan.service;

import com.cskaoyan.bean.BaseParam;
import com.cskaoyan.bean.Issue;
import com.cskaoyan.bean.IssueExample;
import com.cskaoyan.bean.zhao_order_bean.BasePageData;
import com.cskaoyan.mapper.IssueMapper;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <h3>market</h3>
 *
 * @author:Zhao
 * @date:2021-10-20 16:07:04
 * @description:\
 */
@Service
public class IssueServiceImpl implements IssueService{
    @Autowired
    IssueMapper issueMapper;

    @Override
    public BasePageData query(String question, BaseParam param) {
        //配置分页信息 → 在执行预编译过程，会帮你拼接分页的sql语句
        PageHelper.startPage(param.getPage(), param.getLimit());

        IssueExample example = new IssueExample();
        IssueExample.Criteria criteria = example.createCriteria();

        if (question != null && !"".equals(question)) {
            criteria.andQuestionLike("%" + question + "%");
        }

        criteria.andDeletedEqualTo(false);

        example.setOrderByClause(param.getSort() + " " + param.getOrder());

        List<Issue> issues = issueMapper.selectByExample(example);

        return BasePageData.ok(issues);
    }

    @Override
    public Issue create(String answer, String question) {
        Issue issue1 = new Issue();

        issue1.setQuestion(question);
        issue1.setAnswer(answer);

        issueMapper.create(issue1);

        Issue issue = issueMapper.selectByPrimaryKey(issue1.getId());

        return issue;
    }

    @Override
    public Issue update(Integer id, String answer, String question) {
        issueMapper.update(id, answer, question);

        Issue issue = issueMapper.selectByPrimaryKey(id);
        return issue;
    }

    @Override
    public void delete(Integer id) {
        issueMapper.logicDelete(id);//逻辑删除
    }
}
